gpNamespace.netStruct = gpNamespace.netStruct || {};
gpNamespace.netStruct.SUB_C_CARDTYPE = 0x3057;                 //牌型(新增)
gpNamespace.netStruct.SUB_C_PROMPT = 0x3064;                 //提示(新增)
gpNamespace.netStruct.SUB_C_SHOW_CARDS = 12;                 //明牌
gpNamespace.netStruct.SUB_C_TRUSTEE = 4;                  //用户托管
gpNamespace.netStruct.SUB_C_CALLGRAB = 5;                  //抢地主
gpNamespace.netStruct.SUB_C_GAMEREADY = 7;                  //包厢继续
gpNamespace.netStruct.SUB_S_OUT_CARD = 101;                 //服务器端出牌(修改)

gpNamespace.netStruct.SUB_C_OUT_CARD = 0x3063;                 //客户端出牌(新增)
gpNamespace.netStruct.SUB_S_OUT_CARD_NOTIFY = 1;        //出牌提示
gpNamespace.netStruct.SUB_S_START_SEND_CARD = 0x3065;	     //游戏开始

gpNamespace.netStruct.GET_TILES = 102;		    //亮三张底牌
gpNamespace.netStruct.SUB_S_GAME_END = 106;		        //游戏结束
gpNamespace.netStruct.SUB_S_TRUSTEE = 107;			    //用户托管

//新增功能
gpNamespace.netStruct.SUB_C_SEND_USER_GOOD = 108;			    //聊天
gpNamespace.netStruct.SUB_S_CURRENTROOMCOUNT = 0;			    //改变用户数量
gpNamespace.netStruct.SUB_C_SEND_USER_GOOD = 110;         //发送桌上消息

gpNamespace.netStruct.SUB_S_MATCHSTART = 117;               //比赛场开始,兑换券
gpNamespace.netStruct.SUB_S_MATCHEND = 116;               //比赛场结束最后
//gpNamespace.netStruct.SUB_S_MATCHEND = 110;			    //比赛评分，输赢
//gpNamespace.netStruct.SUB_S_MATCHSTART = 110;			    //跳到原来的页面


/**
 * @class
 * @extends cc.Class  基本结构体 基类
 */
gpNamespace.BaseStruct = cc.Class.extend(/** @lends gpNamespace.BaseStruct */{
    ctor: function () {
        this._super();
    },
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    serialize: function (data) {
        if (!data) {
            data = new xinyou.Packet();
        }
        return data;
    },
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    deserialize: function (data) {
        this.readBody(data);
        if (data.bytesAvailable)
            throw this + '还有 ' + data.bytesAvailable + ' 个字节未读取';
    },
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    readBody: function (data) {
    }
});

/**
 * @class
 * @extends gpNamespace.BaseStruct  基本结构体 基类
 */
gpNamespace.NetStruct = gpNamespace.BaseStruct.extend(/** @lends gpNamespace.NetStruct */{
    msgCode: 0,
    /**
     * @param  msgCode   消息代码
     */
    ctor: function (msgCode) {
        this.msgCode = msgCode;
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct  登陆成功
 */
gpNamespace.LoginSuccess = gpNamespace.NetStruct.extend(/** @lends gpNamespace.LoginSuccess */{});

/**
 * @class
 * @extends gpNamespace.NetStruct  登陆失败
 */
gpNamespace.LoginFail = gpNamespace.NetStruct.extend(/** @lends gpNamespace.LoginFail */{
    errorCode: 0,
    errorDesc: '',
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    readBody: function (data) {
        this.errorCode = data.readInt();
        this.errorDesc = data.readString();
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct   开局发牌
 */
gpNamespace.GetTiles = gpNamespace.NetStruct.extend(/** @lends gpNamespace.GetTiles */{

    /**
     * @param {UnsignedShort} zhuangjiaUser 地主用户
     */
    zhuangjiaUser: null,

    /**
     * @param {Array gpNamespace.Tile} tileArray 解析后的麻将数组
     */
    tileArray: [],


    /**
     * @param {xinyou.Packet} data  服务器数据，牌的解析
     */
    readBody: function (data) {
        this.zhuangjiaUser = data.readByte();

        this.tileArray = [];
        var tile = null;
        
        var tiles = [];
        for (var j = 0; j < gpNamespace.GAME_CARD_COUNT; j++) {
            tile = gpNamespace.CardActions.getTileByNum(data.readUnsignedByte());
            if (tile != null)
                tiles[j] = tile;
            cc.log("手牌:" + (j + 1) + "张，牌型:" + tiles[j].num + "-" + tiles[j].type);
        }
        this.tileArray[0] = tiles;
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct   明牌
 */
gpNamespace.GetMingPai = gpNamespace.NetStruct.extend(/** @lends gpNamespace.GetOutTile */{
    /**
     * 倍数  byte
     */
    bei: null,
    /**
     * 明牌用户  byte
     */
    mingUser: null,
    /**
     * 扑克  byte[]
     */
    cards: [],

    readBody: function (data) {
        var len = data.length;
        this.bei = data.readByte();
        this.mingUser = data.readByte();
        this.cards = [];
        
        for (var a = 0; a < len - 2; a++) {
            var tile = gpNamespace.CardActions.getTileByNum(data.readUnsignedByte());
            if (tile != null)
            	this.cards[a] = tile;
        }
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct   出牌
 */
gpNamespace.GetOutTile = gpNamespace.NetStruct.extend(/** @lends gpNamespace.GetOutTile */{
    /**
     * @param {bool} isHuazhu  现在是不是花猪
     */
    cardType: null,
    /**
     * @param {gpNamespace.Tile} myTile  解析后的出牌数据
     */
    myTile: [],
    /**
     * @param {UnsignedShort} outUser  服务器向客户端发送的    出牌的用户
     */
    outUser: null,
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    readBody: function (data) {
        this.cardType = data.readByte();
        this.outUser = data.readByte();

        this.myTile = [];
        var available = data.bytesAvailable;
        for (var i = 0; i < available; i++) {
            var tile = gpNamespace.CardActions.getTileByNum(data.readUnsignedByte());
            if (tile != null)
                this.myTile[i] = tile;
        }
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct   用户托管
 */
gpNamespace.GetTuoguan = gpNamespace.NetStruct.extend(/** @lends gpNamespace.GetTuoguan */{
    /**
     * @param {bool} isTuoguan  是否托管
     */
    isTuoguan: null,
    /**
     * @param {UnsignedShort} tuoguanUser  托管用户
     */
    tuoguanUser: null,
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    readBody: function (data) {
        this.isTuoguan = data.readBoolean();
        this.tuoguanUser = data.readUnsignedShort();
    }
});

/**
 * @class
 * @extends gpNamespace.NetStruct   游戏结束
 */
gpNamespace.GetGameEndInfo = gpNamespace.NetStruct.extend({
	/**
	 * 当前剩余次数
	 */
	currentRemain:null,
	/**
	 * 当前剩余手牌张数
	 */
	remainCardCount:[],
	/**
	 * 当前剩余手牌
	 */
	remainCards:[],
	/**
	 * 输赢的张数
	 */
	winorloseCardsCount:[],
	/**
	 * 玩家倍数
	 */
	userMultiple:[],
	/**
	 * 当前第几轮
	 */
	currentStep:null,
	/**
	 * 当前次数
	 */
	currentTime:null,

	isChunTian: false,
	winIndex: null,

	/**
	 * 游戏积分 int [4]
	 */
	gameScore: [],


	readBody: function (data) {
//		this.currentRemain = data.readByte();
//		this.currentStep = data.readByte();
//		this.currentTime = data.readByte();
//
//		this.isChunTian = data.readBoolean();
		this.winIndex = data.readByte();    //winIndex,userMultiple,remainCardCount,gameScore,remainCards,winorloseCardsCount
		
		this.userMultiple = [];
		for (var i = 0; i < 3; i++) {
			this.userMultiple[i] = data.readByte();
			cc.log("this.userMultiple[i]:" + this.userMultiple[i]);
			cc.log("--------------------");
		}

		this.remainCardCount = [];
		for (var i = 0; i < 3; i++) {
			this.remainCardCount[i] = data.readByte();
			cc.log("this.remainCardData[i]:" + this.remainCardCount[i]);
			cc.log("--------------------");
		}

		this.gameScore = [];
		for (var i = 0; i < 3; i++) {
			this.gameScore[i] = data.readInt();

			cc.log("this.gameScore[i]:" + this.gameScore[i]);
			cc.log("--------------------");
		}

		this.remainCards = [];
		for (var i = 0; i < 3; i++) {
			var tiles = [];
			this.remainCards[i] = tiles;

			for (var j = 0; j < this.remainCardCount[i]; j++) {
				var tile = gpNamespace.CardActions.getTileByNum(data.readUnsignedByte());
				if (tile != null)
					tiles[j] = tile;
			}

			cc.log("this.remainCards[i]:" + this.remainCards[i]);
			cc.log("--------------------");
		}
		
		this.winorloseCardsCount = [];
		for (var i = 0; i < 3; i++) {
			this.winorloseCardsCount[i] = data.readInt();
			cc.log("this.WinorloseCards[i]:" + this.winorloseCardsCount[i]);
			cc.log("--------------------");
		}
	}

});

/**
 * @class  抢地主
 * @param {boolean} grab 是否抢,{boolean} useGood 是否用超级抢地主
 */
gpNamespace.callGrab = function (grab, useGood, callGrade) {
    if (!callGrade)
        callGrade = 3;

    var packet = new xinyou.ByteArray();
    packet.writeByte(callGrade);
    packet.writeBoolean(grab);
    packet.writeBoolean(useGood);
    xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_CALLGRAB, packet);
};

/**
 * @class  客户端发送 出牌
 * @param {Byte} tile  出的牌
 */
/**
 * 信息提示参数
 */
messageInfo:null,

gpNamespace.sendOutCards = function (tiles) {
	cc.log("客户端发送 出牌+++++++++");
    var packet = new xinyou.ByteArray();
    for (var i = 0; i < tiles.length; i++) {
    	cc.log(tiles[i]);
        packet.writeByte(gpNamespace.CardActions.getNumByTile(tiles[i]));
    }
    xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_OUT_CARD, packet);
};  

/**
 * @class  顺子，夫佬，同花顺，四条，同花
 * @param {Byte} tile  
 */
gpNamespace.fivePrompt = function (type) {
	cc.log("客户端发送 出牌");
	var packet = new xinyou.ByteArray();


	xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_CARDTYPE, packet);
};  

/**
 * @class  提示
 * @param {Byte} tile  
 */
gpNamespace.prompt = function () {
	cc.log("客户端提示按钮");
	var packet = new xinyou.ByteArray();
	xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_PROMPT, packet);
};  

/**
 * @class  客户端发送 是否托管
 * @param {Bool} isTuoguan  是否托管
 */
gpNamespace.setTuoguan = function (isTuoguan) {
    var packet = new xinyou.ByteArray();
    packet.writeBoolean(isTuoguan);
    xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_TRUSTEE, packet);
};

/**
 * @class  客户端发送 明牌
 */
gpNamespace.sendShowCards = function () {
    var packet = new xinyou.ByteArray();
    packet.writeBoolean(true);
    xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_SHOW_CARDS, packet);
};

/**
 * @class  客户端获取包厢列表
 */
gpNamespace.sendShowCards = function () {
	var packet = new xinyou.ByteArray();
	packet.writeBoolean(true);
	xinyou.netManager.sendGameMessage(gpNamespace.netStruct.SUB_C_SHOW_CARDS, packet);
};


gpNamespace.ReceiveMsg = gpNamespace.NetStruct.extend(/** @lends gpNamespace.LoginFail */{
    /**
     * @param {UnsignedShort} audioIndex  音效人索引
     */
    audioIndex: null,
    /**
     * @param {UnsignedShort} audioID  音效ID
     */
    audioID: null,
    /**
     * @param {UnsignedShort} messageLength  信息长度
     */
    messageLength: null,
    /**
     * @param {COLORREF 4个字节整数} messageColor  信息颜色
     */
    messageColor: null,
    /**
     * @param {UnsignedLong} fromUser  发送用户
     */
    fromUser: null,
    /**
     * @param {UnsignedLong} toUser  目标用户
     */
    toUser: null,
    /**
     * @param {Array byte} messageInfo  聊天信息
     */
    messageInfo: [gpNamespace.MAX_CHAT_LEN],
    /**
     * @param {xinyou.Packet} data  服务器数据
     */
    readBody: function (data) {
        this.audioIndex = data.readUnsignedShort();
        this.audioID = data.readUnsignedShort();
        this.messageLength = data.readUnsignedShort();
        data.readUnsignedShort();
        this.messageColor = data.readInt();
        this.fromUser = data.readInt();
        this.toUser = data.readInt();
        this.messageInfo = data.readString(this.messageLength);
    }
});

gpNamespace.MAX_CHAT_LEN = 512;
gpNamespace.MessageStruct = {
    /**
     * @param {UnsignedShort} audioIndex  音效人索引
     */
    audioIndex: null,
    /**
     * @param {UnsignedShort} audioID  音效ID
     */
    audioID: null,
    /**
     * @param {UnsignedShort} messageLength  信息长度
     */
    messageLength: null,
    /**
     * @param {COLORREF 4个字节整数} messageColor  信息颜色
     */
    messageColor: null,
    /**
     * @param {UnsignedLong} fromUser  发送用户
     */
    fromUser: null,
    /**
     * @param {UnsignedLong} toUser  目标用户
     */
    toUser: null,
    /**
     * @param {Array byte} messageInfo  聊天信息
     */
    messageInfo: [gpNamespace.MAX_CHAT_LEN]
};

gpNamespace.reconnect = gpNamespace.NetStruct.extend(/** @lends gpNamespace.LoginFail */{
	
	//断线重连
	currentAt:null,          //出牌玩家...
	lastChuCardsType:null,   //上家出的牌型
	lastChuPlayer:null,      //上一次出牌玩家
	status:null,			//当前重连玩家状态
	cardsCount:[],           //剩余牌的张数...
	lastChuCards:[],         //上家出的牌
	playerStates:[],		//其他玩家的状态
	usersCards:[],           //手牌...
	
	readBody: function (data) {
		var available = data.bytesAvailable;
		this.currentAt = data.readByte();
		this.lastChuCardsType = data.readByte();
		this.lastChuPlayer = data.readByte();
		this.status = data.readByte();
		
		//剩余牌的张数
		for (var j = 0; j < 3; j++) {
			this.cardsCount[j] = data.readByte();
		}
		
		//上家出的牌
		this.lastChuCards = [];
		for (var i = 0; i < 16; i++) {
			this.lastChuCards[i] = data.readByte();
		}
		
		//玩家状态
		for (var i = 0; i < 3; i++) {
			this.playerStates[i] = data.readByte();
		}
		
		for (var j = 0; j < 16; j++) {
			var tile = gpNamespace.CardActions.getTileByNum(data.readUnsignedByte());
			if (tile != null)
				this.usersCards[j] = tile;
//			cc.log("this.usersCards[j]" +this.usersCards[j]);
//			cc.log("----------------------------------------------");
		}
	}
});

gpNamespace.GetUserGoodInfo = gpNamespace.NetStruct.extend({
	/**
	 * 发送人 byte
	 */
	sender: null,
	/**
	 * 接收人 byte
	 */
	receiver: null,
	/**
	 * 商品ID int
	 */
	goodID: null,

	readBody: function (data) {
		this.sender = data.readByte();
		this.receiver = data.readByte();
		this.goodID = data.readInt();
	}
});

gpNamespace.table = gpNamespace.NetStruct.extend({
	
	id: null,
	
	baseScore: null,
	
	bixia:null,
	
	count: null,
	
	status: null,
	
	name: null,
	
	password: null,
	
	readBody: function (data) {
		this.baseScore = data.readInt();
		this.biXia = data.readInt();
		this.count = data.readByte();
		this.id = data.readInt();
		this.status = data.readByte();
		this.name = data.readMyString();
		this.password = data.readMyString();
	}

});